<template>
  <div class="maintenance-form-container">
    <el-card class="form-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <span class="card-title">维护项目表单管理</span>
          <el-button type="primary" :icon="CirclePlus" @click="handleAdd">新增表单</el-button>
        </div>
      </template>

      <el-table :data="tableData" style="width: 100%" v-loading="loading" border>
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="formName" label="表单名称" />
        <el-table-column prop="projectName" label="关联项目" />
        <el-table-column prop="formType" label="表单类型">
          <template #default="scope">
            <el-tag :type="getFormTypeColor(scope.row.formType)">{{ getFormTypeText(scope.row.formType) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="fieldCount" label="字段数量" />
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            <el-tag :type="scope.row.status === 'ACTIVE' ? 'success' : 'warning'">
              {{ scope.row.status === 'ACTIVE' ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createdTime" label="创建时间" />
        <el-table-column label="操作" width="250" fixed="right">
          <template #default="scope">
            <el-button type="primary" link :icon="View" @click="handlePreview(scope.row)">预览</el-button>
            <el-button type="primary" link :icon="EditPen" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="success" link :icon="Setting" @click="handleDesign(scope.row)">设计</el-button>
            <el-button type="danger" link @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup lang="ts" name="maintenanceForm">
import { ref, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { CirclePlus, EditPen, View, Setting } from "@element-plus/icons-vue";

const loading = ref(false);
const tableData = ref([]);

// 获取表单类型颜色
const getFormTypeColor = (type: string) => {
  const typeMap: Record<string, string> = {
    CHECKLIST: "primary",
    INSPECTION: "success",
    MAINTENANCE: "warning",
    REPAIR: "danger"
  };
  return typeMap[type] || "info";
};

// 获取表单类型文本
const getFormTypeText = (type: string) => {
  const typeMap: Record<string, string> = {
    CHECKLIST: "检查清单",
    INSPECTION: "巡检表单",
    MAINTENANCE: "保养表单",
    REPAIR: "维修表单"
  };
  return typeMap[type] || type;
};

// 获取表格数据
const getTableData = async () => {
  loading.value = true;
  try {
    // TODO: 调用API获取维护表单数据
    console.log("获取维护表单数据");
  } catch (error) {
    console.error("获取维护表单列表失败:", error);
    ElMessage.error("获取维护表单列表失败");
  } finally {
    loading.value = false;
  }
};

// 新增
const handleAdd = () => {
  ElMessage.info("新增维护表单功能待实现");
};

// 预览
const handlePreview = (row: any) => {
  ElMessage.info("预览维护表单功能待实现");
};

// 编辑
const handleEdit = (row: any) => {
  ElMessage.info("编辑维护表单功能待实现");
};

// 设计
const handleDesign = (row: any) => {
  ElMessage.info("设计维护表单功能待实现");
};

// 删除
const handleDelete = (row: any) => {
  ElMessageBox.confirm(`确认删除维护表单 "${row.formName}" 吗？`, "删除确认", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      ElMessage.success("删除成功");
      getTableData();
    })
    .catch(() => {});
};

// 页面加载时获取数据
onMounted(() => {
  getTableData();
});
</script>

<style scoped>
.maintenance-form-container {
  padding: 20px;
}

.form-card {
  border-radius: 8px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-title {
  font-size: 16px;
  font-weight: 600;
}
</style>